// pages/address/edit.js
Page({

    /**
     * 页面的初始数据
     */
    //新建地址时默认值
    data: {
        userInfo: {},
        tempAddress: {
            addressId: 0,
            userId: 0,
            name: "",
            phone: "",
            addressProvince: "",
            addressCity: "",
            addressDetail: "",
            isDefault: 0,
            createTime: "",
            updateTime: ""
        }

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //console.log(options)
        if (options.address) {
            this.setData({
                tempAddress: JSON.parse(options.address)
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        const userinfo = wx.getStorageSync("userInfo");
        this.setData({
            userInfo: userinfo
        });
    },


    //验证方法
    checkAddress() {
        var address = this.data.tempAddress;
        var tipStr = "";
        if (address.name.length == 0) {
            tipStr = "请输入收货人姓名";
        } else if (address.phone.length == 0) {
            tipStr = "请输入收货人手机号";
        } else if (address.addressProvince.length == 0) {
            tipStr = "请选择所在地址";
        } else if (address.addressDetail.length == 0) {
            tipStr = "请填写详细地址";
        }
        if (tipStr.length == 0) {
            return true;
        } else {
            wx.showToast({
                icon: 'none',
                title: tipStr,
            })
            return false;
        }
    },
    inputName(e) {
        this.data.tempAddress.name = e.detail.value;
        this.setData({
            tempAddress: this.data.tempAddress
        })
    },
    inputMobile(e) {
        this.data.tempAddress.phone = e.detail.value;
        this.setData({
            tempAddress: this.data.tempAddress
        })
    },
    inputStreet(e) {
        this.data.tempAddress.addressDetail = e.detail.value;
        this.setData({
            tempAddress: this.data.tempAddress
        })
    },
    //选择所在地址
    bindRegionChange(e) {
        var city = e.detail.value;
        this.data.tempAddress.addressProvince = city.join(" ");
        this.setData({
            tempAddress: this.data.tempAddress
        })
    },
    clickDefault() {
        if (this.data.tempAddress.isDefault) {
            this.data.tempAddress.isDefault = false;
        } else {
            this.data.tempAddress.isDefault = true;
        }
        this.setData({
            tempAddress: this.data.tempAddress
        })
    },
    clickSave(e) {
        var _this = this
        if (!this.checkAddress()) {
            return
        }
        var addressList = wx.getStorageSync('addressList');
        var address = _this.data.tempAddress;
        var isAdd = false;
        var addressListNew = [];
        var indexDefault = -1; //默认选中地址的索引
        var indexCurrent = -1; //如果是编辑地址，获取当前编辑地址在地址列表中的索引

        console.log(address.addressId);
        if (address.addressId == 0) {
            _this.addAddress();
            // isAdd = true;
            // if (addressList) {
            //     addressList.forEach(function (v, index) {
            //         if (v.isDefault) {
            //             indexDefault = index;
            //         }
            //         // 如果当前编辑的地址为默认选中地址，将其他所有地址选中状态清除
            //         if (address.isDefault) {
            //             v.isDefault = false;
            //         }
            //     })
            // }
            // addressListNew = [address, ...addressList];
            // indexCurrent = 0;
        } else {
            _this.updataAddress()
            //编辑地址
            // addressList = Array.from(addressList);
            // addressList.forEach(function (v, index) {
            //     if (v.isDefault) {
            //         indexDefault = index;
            //     }
            //     // 如果当前编辑的地址为默认选中地址，将其他所有地址选中状态清除
            //     if (address.isDefault) {
            //         v.isDefault = false;
            //     }
            //     //如果为同一个地址，给旧地址赋值
            //     if (address.id == v.id) {
            //         v.name = address.name;
            //         v.mobile = address.mobile;
            //         v.city = address.city;
            //         v.street = address.street;
            //         v.isDefault = address.isDefault;
            //         indexCurrent = index;
            //     }
            // })
            // addressListNew = addressList;
        }
        // //如果新地址没有默认选中地址，将当前地址设置为默认选中地址
        // if (indexDefault == -1) {
        //     addressListNew[indexCurrent].isDefault = true;
        // } else {
        //     //如果编辑地址有选中默认地址 并且默认选中地址和当前编辑地址是同一个，那么将当前地址设置为默认地址
        //     if (indexDefault == indexCurrent && !isAdd) {
        //         addressListNew[indexCurrent].isDefault = true;
        //     }
        // }
        // wx.setStorageSync('addressList', addressListNew);
        wx.showToast({
            title: '保存成功！',
            icon: 'success',
            success() {
                wx.navigateBack({
                    delta: 1,
                    success: (res) => { },
                    fail: (res) => { },
                    complete: (res) => { },
                })
            }
        })
    },
    addAddress: function (e) {
        var _this = this;
        this.data.tempAddress.userId = this.data.userInfo.userId;
        this.setData({
            tempAddress: this.data.tempAddress
        })
        wx.showLoading({
          title: '加载中',
          mask: true
        })
        var newAddress = this.data.tempAddress;
        wx.request({
            url: 'http://43.139.97.111:8080/address/insert',
            data: {
                addressId: 0,
                userId: newAddress.userId,
                name: newAddress.name,
                phone: newAddress.phone,
                addressProvince: newAddress.addressProvince,
                addressCity: "",
                addressDetail: newAddress.addressDetail,
                isDefault: 0,
                createTime: "",
                updateTime: ""
            },
            header: {
                'content-type': 'application/json' // 默认值
            },
            method: 'POST',
            success: function (res) {
                console.log("res", res);
            },
            fail: function (res) {},
            complete: function (res) {},
        })
        wx.hideLoading()
      },

      updataAddress(){
        var _this = this;
        this.data.tempAddress.userId = this.data.userInfo.userId;
        this.setData({
            tempAddress: this.data.tempAddress
        })
        wx.showLoading({
          title: '加载中',
          mask: true
        })
        var newAddress = this.data.tempAddress;
        wx.request({
            url: 'http://43.139.97.111:8080/address/update',
            data: {
                addressId: newAddress.addressId,
                userId: newAddress.userId,
                name: newAddress.name,
                phone: newAddress.phone,
                addressProvince: newAddress.addressProvince,
                addressCity: newAddress.addressCity,
                addressDetail: newAddress.addressDetail,
                isDefault: 0,
            },
            header: {
                'content-type': 'application/json' // 默认值
            },
            method: 'PUT',
            success: function (res) {
                console.log("res", res);
            },
            fail: function (res) {},
            complete: function (res) {},
        })
        wx.hideLoading()

        
      }
})